from sqlmodel import SQLModel, Field
from datetime import datetime
from typing import Optional


class Rate(SQLModel, table=True):
    """汇率数据表"""
    id: Optional[int] = Field(default=None, primary_key=True)
    ts_utc: datetime = Field(index=True)
    base: str = Field(default="JPY", max_length=3)
    quote: str = Field(max_length=3)
    rate: float = Field(description="1 base 可兑换 quote 的数量")
    
    class Config:
        # 确保时间戳+货币对唯一
        indexes = [
            ("ts_utc", "base", "quote", True)  # unique index
        ]


class RateResponse(SQLModel):
    """API响应模型"""
    ts_utc: datetime
    base: str
    quote: str
    rate: float
    amount_1000: float = Field(description="1000 base 可兑换的 quote 数量")


class LatestRatesResponse(SQLModel):
    """最新汇率响应"""
    jpy_cny: RateResponse
    jpy_usd: RateResponse
    last_updated: datetime


class HealthResponse(SQLModel):
    """健康检查响应"""
    status: str
    timestamp: datetime
    database_connected: bool
